package de.akabahn.ebd.control;

import java.io.PrintWriter;
import java.io.StringWriter;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class EBD_DefaultUncaughtExceptionHandler extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

        Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
            @Override
            public void uncaughtException(Thread paramThread, Throwable exception) {
                StringWriter stackTrace = new StringWriter();
                exception.printStackTrace(new PrintWriter(stackTrace));

                StringBuilder errormessage = new StringBuilder(exception.getMessage());
            	while (exception.getCause() != null) {
            		exception = exception.getCause();
            		errormessage.append("\n\t\t- ").append(exception.getMessage());
            	}

                Intent loginIntent = new Intent(EBD_DefaultUncaughtExceptionHandler.this, EBD_LoginActivity.class);
                loginIntent.putExtra("de.akabahn.ebd.control.errormessage", errormessage.toString());
                loginIntent.putExtra("de.akabahn.ebd.control.stacktrace", stackTrace.toString());
                loginIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
                loginIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(loginIntent);
                System.exit(0);
            }
        });
	}
}
